package com.fancy.learncenter.ui.view.date.custom;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;


import com.fancy.learncenter.R;
import com.fancy.learncenter.ui.view.date.AbstractWheel;
import com.fancy.learncenter.ui.view.date.OnWheelChangedListener;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by linhuasen on 15/12/25.
 */
public class DateTimePicker extends LinearLayout{

    public static final int DATE_PICKER = 1; // year-month-day

    public static final int DATE_PICKER_NO_YEAR = 2; // month-day

    public static final int DATE_PICKER_NO_DAY = 3; // month-day

    public static final int TIME_PICKER = 4; // hour:minus:second

    public static final int TIME_PICKER_NO_SECOND = 5; // hour:minus

    public static final int DATE_TIME_PICKER = 6; // year-month-day hour:minus

    public static final int DATE_TIME_PICKER_FULL = 7; // year-month-day hour:minus:second

    public static int START_YEAR = 1900;
    public static int END_YEAR = 2100;

    String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
    String[] months_little = { "4", "6", "9", "11" };
    final List<String> list_big = Arrays.asList(months_big);
    final List<String> list_little = Arrays.asList(months_little);
    private Context mContext;

    private AbstractWheel yearWheel;
    private AbstractWheel monthWheel;
    private AbstractWheel dayWheel;
    private AbstractWheel hourWheel;
    private AbstractWheel minusWheel;
    private AbstractWheel secondWheel;

    private YearArrayAdapter yearAdapter;
    private MonthArrayAdapter monthAdapter;
    private DayArrayAdapter dayAdapter;
    private TimeArrayAdapter hourAdapter;
    private TimeArrayAdapter minusAdapter;
    private TimeArrayAdapter secondAdapter;

    public DateTimePicker(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public DateTimePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public DateTimePicker(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init(){

        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_date_time_picker,this);
        yearWheel = (AbstractWheel)view.findViewById(R.id.year);
        yearWheel.setVisibleItems(5);
        yearAdapter = new YearArrayAdapter(mContext);
        yearWheel.setViewAdapter(yearAdapter);

        monthWheel = (AbstractWheel)view.findViewById(R.id.month);
        monthWheel.setVisibleItems(5);
        monthAdapter = new MonthArrayAdapter(mContext);
        monthWheel.setViewAdapter(monthAdapter);

        dayWheel = (AbstractWheel)view.findViewById(R.id.day);
        dayWheel.setVisibleItems(5);
        dayAdapter = new DayArrayAdapter(mContext,1,31);
        dayWheel.setViewAdapter(dayAdapter);

        hourWheel = (AbstractWheel)view.findViewById(R.id.hour);
        hourWheel.setVisibleItems(5);
        hourAdapter = new TimeArrayAdapter(mContext,0,23,"时");
        hourWheel.setViewAdapter(hourAdapter);

        minusWheel = (AbstractWheel)view.findViewById(R.id.minus);
        minusWheel.setVisibleItems(5);
        minusAdapter = new TimeArrayAdapter(mContext,0,59,"分");
        minusWheel.setViewAdapter(minusAdapter);

        secondWheel = (AbstractWheel)view.findViewById(R.id.second);
        secondWheel.setVisibleItems(5);
        secondAdapter = new TimeArrayAdapter(mContext,0,59,"秒");
        secondWheel.setViewAdapter(secondAdapter);

        yearWheel.addChangingListener(yearWheelChangedListener);
        monthWheel.addChangingListener(monthWheelChangedListener);

        setTime(Calendar.getInstance());
    }

    public void configDateTimePicker(int type){
        if(type == DATE_PICKER){// year-month-day
            yearWheel.setVisibility(View.VISIBLE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.VISIBLE);
            hourWheel.setVisibility(View.GONE);
            minusWheel.setVisibility(View.GONE);
            secondWheel.setVisibility(View.GONE);
        }else if(type == DATE_PICKER_NO_YEAR){// month-day
            yearWheel.setVisibility(View.GONE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.VISIBLE);
            hourWheel.setVisibility(View.GONE);
            minusWheel.setVisibility(View.GONE);
            secondWheel.setVisibility(View.GONE);
        }else if(type == DATE_PICKER_NO_DAY){// year-month
            yearWheel.setVisibility(View.VISIBLE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.GONE);
            hourWheel.setVisibility(View.GONE);
            minusWheel.setVisibility(View.GONE);
            secondWheel.setVisibility(View.GONE);
        }else if(type == TIME_PICKER){// hour:minus:second
            yearWheel.setVisibility(View.GONE);
            monthWheel.setVisibility(View.GONE);
            dayWheel.setVisibility(View.GONE);
            hourWheel.setVisibility(View.VISIBLE);
            minusWheel.setVisibility(View.VISIBLE);
            secondWheel.setVisibility(View.VISIBLE);
        }else if(type == TIME_PICKER_NO_SECOND){// hour:minus
            yearWheel.setVisibility(View.GONE);
            monthWheel.setVisibility(View.GONE);
            dayWheel.setVisibility(View.GONE);
            hourWheel.setVisibility(View.VISIBLE);
            minusWheel.setVisibility(View.VISIBLE);
            secondWheel.setVisibility(View.GONE);
        }else if(type == DATE_TIME_PICKER){// year-month-day hour:minus
            yearWheel.setVisibility(View.VISIBLE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.VISIBLE);
            hourWheel.setVisibility(View.VISIBLE);
            minusWheel.setVisibility(View.VISIBLE);
            secondWheel.setVisibility(View.GONE);
        }else if(type == DATE_TIME_PICKER_FULL){
            yearWheel.setVisibility(View.VISIBLE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.VISIBLE);
            hourWheel.setVisibility(View.VISIBLE);
            minusWheel.setVisibility(View.VISIBLE);
            secondWheel.setVisibility(View.VISIBLE);
        }else{
            yearWheel.setVisibility(View.VISIBLE);
            monthWheel.setVisibility(View.VISIBLE);
            dayWheel.setVisibility(View.VISIBLE);
            hourWheel.setVisibility(View.VISIBLE);
            minusWheel.setVisibility(View.VISIBLE);
            secondWheel.setVisibility(View.VISIBLE);
        }

    }
    // 添加"年"监听
    private OnWheelChangedListener yearWheelChangedListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
            int year_num = newValue + START_YEAR;
            // 判断大小月及是否闰年,用来确定"日"的数据
            if (list_big.contains(String.valueOf(monthWheel.getCurrentItem() + 1))) {
                dayAdapter = new DayArrayAdapter(mContext,1,31);
                dayWheel.setViewAdapter(dayAdapter);
            } else if (list_little.contains(String.valueOf(monthWheel.getCurrentItem() + 1))) {
                dayAdapter = new DayArrayAdapter(mContext,1,30);
                dayWheel.setViewAdapter(dayAdapter);
            } else {
                if ((year_num % 4 == 0 && year_num % 100 != 0)|| year_num % 400 == 0){
                    dayAdapter = new DayArrayAdapter(mContext,1,29);
                    dayWheel.setViewAdapter(dayAdapter);
                }else{
                    dayAdapter = new DayArrayAdapter(mContext,1,28);
                    dayWheel.setViewAdapter(dayAdapter);
                }
            }
        }
    };

    // 添加"月"监听
    private OnWheelChangedListener monthWheelChangedListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
            int month_num = newValue + 1;
            // 判断大小月及是否闰年,用来确定"日"的数据
            if (list_big.contains(String.valueOf(month_num))) {
                dayAdapter = new DayArrayAdapter(mContext,1,31);
                dayWheel.setViewAdapter(dayAdapter);
            } else if (list_little.contains(String.valueOf(month_num))) {
                dayAdapter = new DayArrayAdapter(mContext,1,30);
                dayWheel.setViewAdapter(dayAdapter);
            } else {
                if (((yearWheel.getCurrentItem() + START_YEAR) % 4 == 0 && (yearWheel
                        .getCurrentItem() + START_YEAR) % 100 != 0)
                        || (yearWheel.getCurrentItem() + START_YEAR) % 400 == 0){
                    dayAdapter = new DayArrayAdapter(mContext,1,29);
                    dayWheel.setViewAdapter(dayAdapter);
                }else{
                    dayAdapter = new DayArrayAdapter(mContext,1,28);
                    dayWheel.setViewAdapter(dayAdapter);
                }
            }
        }
    };

    public void setTime(Calendar calendar) {
        if (calendar == null)
            return;
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minus = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);

        yearWheel.setCurrentItem(year - DateTimePicker.START_YEAR);// 初始化时显示的数据
        monthWheel.setCurrentItem(month);
        // 判断大小月及是否闰年,用来确定"日"的数据
        if (list_big.contains(String.valueOf(month + 1))) {
            dayAdapter = new DayArrayAdapter(mContext,1,31);
            dayWheel.setViewAdapter(dayAdapter);
        } else if (list_little.contains(String.valueOf(month + 1))) {
            dayAdapter = new DayArrayAdapter(mContext,1,30);
            dayWheel.setViewAdapter(dayAdapter);
        } else {
            // 闰年
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
                dayAdapter = new DayArrayAdapter(mContext,1,29);
                dayWheel.setViewAdapter(dayAdapter);
            }else{
                dayAdapter = new DayArrayAdapter(mContext,1,28);
                dayWheel.setViewAdapter(dayAdapter);
            }

        }
        dayWheel.setCurrentItem(day - 1);

        hourWheel.setCurrentItem(hour);
        minusWheel.setCurrentItem(minus);
        secondWheel.setCurrentItem(second);
    }

    public Calendar getCalendar() {
        // TODO Auto-generated method stub
        Calendar c = Calendar.getInstance();
        c.set(yearWheel.getCurrentItem() + DateTimePicker.START_YEAR, monthWheel.getCurrentItem(),
                dayWheel.getCurrentItem() + 1,hourWheel.getCurrentItem(),minusWheel.getCurrentItem(),secondWheel.getCurrentItem());
        return c;
    }

    public void setTime(String time){
        setTime(time,"yyyy-MM-dd HH:hh:ss");
    }

    public void setTime(String time,String parten){
        SimpleDateFormat sdf = new SimpleDateFormat(parten);
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
            date = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        setTime(calendar);
    }

    public void setTime(long time){
        Date date = new Date(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        setTime(calendar);
    }

    public long getTime(){
        return getCalendar().getTimeInMillis();
    }

    public String getTextStr() {
        return getFormatTime(getCalendar());
    }

    private String getFormatTime(Calendar c) {
        String parten = "00";
        DecimalFormat decimal = new DecimalFormat(parten);
        // 设置日期的显示
        Calendar calendar = c;
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minus = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return year + "-"
                + decimal.format(month + 1) + "-"
                + decimal.format(day) + " "
                + decimal.format(hour) + ":"
                + decimal.format(minus) + ":"
                + decimal.format(second);

    }



}
